- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4
feat: gas calculation pattern #619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| To fix the formatting issues: 
 npx remark -o --silent --silently-ignore guidebook/debug.mdx techniques/gas.mdx  | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice PR, can we add link to gas-utility file?
| Thanks for the update to the techniques docs. One high‑severity fix is needed before merge. Findings (1)High (1)[HIGH] Missing frontmatter; page title rendered as body textDescription: Suggestion: -## title: "Estimate gas usage in TON contracts"
+---
+title: "Estimate gas usage in TON contracts"
+--- | 
| To fix the formatting issues: 
 npx remark -o --silent --silently-ignore guidebook/debug.mdx techniques/gas.mdx  | 
| To fix the formatting issues: 
 npx remark -o --silent --silently-ignore guidebook/debug.mdx standard/tokens/airdrop.mdx techniques/gas.mdx  | 
| To fix the formatting issues: 
 npx remark -o --silent --silently-ignore guidebook/debug.mdx standard/tokens/airdrop.mdx techniques/gas.mdx  | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice pr!
Can we add may be more pics
I have several suggestions:
| @skywardboundd sure, examples in ts will be added as part of #620 | 
| To fix the formatting issues: 
 npx remark -o --silent --silently-ignore guidebook/debug.mdx techniques/gas.mdx  | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's fix the issues by @skywardboundd and the AI review issues too
| /review | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR—I've left several suggestions in techniques/gas.mdx: please apply the inline suggestions.
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems good to me!
| Also there is a task on rewriting examples on Tolk. I am not sure that it will make the article better, so this issue will be closed in another PR. #1003 | 
        71588c0
      
    | /review | 
| Re-deployed: View deployment I'm not sure if Mintlify's bot will pick up new updates here, because previously deployments were skipped for this branch and all other branches that were moved from the previous org/repo place. | 
| A,B,80 | ||
| A,Storage fee (A),5 | ||
| A,Compute fee (A),15 | ||
| A,Forward fee (A->B),20 | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the A's remaining balance?
| This article covers abstract contracts system, not connected to any existing project. However, it's primary applicable to contract systems that uses [carry-value pattern.](/contract-dev/carry-value) | ||
| </Aside> | ||
|  | ||
| It is crucial to understand that there is no separate message balance and contract balance. After the message is received, coins are added to the contract balance, and then the contract is executed. Sending message mods and reserve actions help to properly divide contract balance in the action phase. This diagram of a possible value flow illustrates this. Note, that this diagram is not connected to the diagram above, it illustrates different fact. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the message is received, coins are added to the contract balance, and then the contract is executed.
This is not absolutly true. It depends on an order of the storage and credit phases.
|  | ||
| The reason for this requirement is that reverting the contract system state is usually not possible, because the toncoins are already spent. | ||
|  | ||
| In case you are writing a contract system, where correctness depends on successful execution of the rest of the transaction trace, then you need to guarantee that there are enough attached toncoins in an incoming message to cover all fees. This article describes how to compute those fees. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are writting
We don't talk to a reader. This is a documentation, it describes facts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many such cases in the article, please handle them all.
|  | ||
| ComputeDataSize have the second argument - maximum number of cells to visit. If it is ok to set in in _8192_ since it is the [limit for message size](/foundations/limits#message-and-transaction-limits). | ||
|  | ||
| ```tact | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to check that the calculation really works as expected. We discussed it personally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that it should be:
let sizeBody = computeDataSize(msg.body.toCell(), 8192);
let sizeStateInit = computeDataSize(msg.init.toCell(), 8192);
let fwdFee = getForwardFee(sizeBody.cells + sizeStateInit.cells - 2, sizeBody.bits + sizeStateInit.bits - msg.body.toCell().bits() - msg.init.toCell().bits(), isAccountInMasterchain);If this is still not true, then you need to clarify exactly how the function getForwardFee() works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, it must be
let sizeMsg = computeDataSize(msg.toCell(), 8192);
let fwdFee = getForwardFee(sizeMsg.cells - 1, sizeMsg.bits - msg.toCell().bits(), isAccountInMasterchain);| fwdFee = basePrice + priceForCells * cells + priceForBits * bits | ||
| ``` | ||
|  | ||
| So, when one want to send message, with `a + b` cells and `x + y` bits, the forward fee won't be `getForwardFee(a + b, x + y)`, but rather `basePrice + priceForCells * (a + b) + priceForBits * (x + y)`. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, when one want to send message
Perhaps, two messages?
|  | ||
| **Approach 2: Cover storage on demand** | ||
|  | ||
| In the worst case the storage fee for a single message is [`freeze_due_limit`](/foundations/config#param-20-and-21%3A-gas-prices). Otherwise, the contract likely is already frozen and a transaction chain is likely to fail anyway. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the worst case the storage fee for a single message is
freeze_due_limit.
It is not clear what is being discussed and what the message has to do with it. Rephrase it.
| Verify the hardcoded contract size in tests. | ||
| </Aside> | ||
|  | ||
| **Approach 2: Cover storage on demand** | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The section is generally poorly written. We need to rethink the narrative again and rewrite it.
| } | ||
| ``` | ||
|  | ||
| This simplifies fee calculation at the start of the operation—you do not need to pre‑calculate storage fees. The `myStorageDue()` function returns the amount needed to bring the balance to zero (or zero if it is already positive). | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
myStorageDue() just outputs the storage due, but not the thing described.
Closes #175